Thread: fgets does not read the complete tab separated line

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    fgets does not read the complete tab separated line

    Hi,

    I have prepared a file through the use of following code
    Code:
    fprintf(file2, "%i\t%i\t%i\t%i\t%i\t%i\n",
    							msg_id,
    							msg_size,
    							msg_period,
    							msg_deadline,
    							msg_producer,
    							msg_comsumer
    					);
    As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
    I face the problem when I read this file, using fgets as follows:
    Code:
    char singleMessage[100];
    
    while( fgets(singleMessage, sizeof(singleMessage), file )  )
    {		
    	puts(singleMessage);
    	sscanf(singleMessage, "%i\t%i\t%i\t%i\t%i\t%i\n",
                              &first, &second, &third, &fourth, &fifth, &sixth);
    	fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d\, sixth: %d\n", 
    first, second, third, fourth, fifth, sixth);
    }
    but fgets only retrieves until the first, i.e, if the first line in the file reads:
    78 800 4 4 2 5
    fgets returns only 78.

    What can be the problem here,
    Does it have to do with how the file was written in the first place.

    thanks,

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried removing the '\t' and the '\n' characters from your sscanf(). By default the scanf() series of functions ignore leading whitespace, and they stop processing a format specifier when they encounter a whitespace.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read inputs separated by space
    By acpower in forum C Programming
    Replies: 2
    Last Post: 04-10-2012, 09:54 AM
  2. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  3. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  4. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  5. Read two words separated by a space with scanf at once
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 06-23-2005, 04:17 PM